<template>
  <div class="lineChart" style="width: 100%; height: 100%"></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    options: {
      default: () => [],
    },
    xAxisData: {
      default: () => [],
    },
  },
  data() {
    return {
      chart: {},
      timer: null,
    };
  },
  watch: {
    options: {
      handler() {
        this.initChart();
      },
      deep: true,
    },
  },
  mounted() {
    this.initChart();
    // 浏览器尺寸发生变化时
    let _this = this;
    window.addEventListener("resize", function () {
      clearTimeout(_this.timer);
      _this.timer = setTimeout(function () {
        _this.chart.resize();
      }, 800);
    });
  },
  methods: {
    fontSize(fz) {
      let docEl = document.documentElement,
        clientWidth =
          window.innerWidth ||
          document.documentElement.clientWidth ||
          document.body.clientWidth;
      if (clientWidth) {
        return (fz * clientWidth) / 1920;
      } else {
        return fz;
      }
    },
    initChart() {
      let series = [];
      for (let i = 0; i < this.options.length; i++) {
        series.push({
          name: this.options[i].name,
          type: "line",
          smooth: true,
          symbol: "circle",
          symbolSize: this.fontSize(12),
          showSymbol: false,
          lineStyle: {
            width: this.fontSize(3),
            shadowColor: "rgba(155, 18, 184, .4)",
            shadowBlur: this.fontSize(5),
            shadowOffsetY: this.fontSize(10),
            shadowOffsetX: 0,
            color: this.options[i].color,
          },
          itemStyle: {
            color: this.options[i].color,
            borderColor: "#fff",
            borderWidth: this.fontSize(2),
          },
          data: this.options[i].data,
        });
      }
      this.chart = echarts.init(document.querySelector(".lineChart"));
      let option = {
        backgroundColor: "#fff",
        tooltip: {
          trigger: "axis",
          show: true,
          confine: true,
          axisPointer: {
            type: "line",
            lineStyle: {
              width: 2,
              color: "#BEBFC3",
            },
          },
          textStyle: {
            fontSize: this.fontSize(14),
          },
        },
        legend: {
          show: true,
          icon: "circle",
          left: "10%",
          top: 0,
          itemWidth: 10,
          //   itemHeight: 10,
          align: "left",
          textStyle: {
            fontSize: this.fontSize(14),
            color: "#333",
            padding: 0,
          },
        },
        grid: {
          left: "0%",
          right: "0%",
          top: "20%",
          bottom: "5%",
          containLabel: true,
        },
        xAxis: {
          axisLine: {
            show: true,
            lineStyle: {
              color: "#d7d7d7",
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "#74798B",
            fontSize: this.fontSize(14),
          },
          data: this.xAxisData,
        },
        yAxis: {
          axisLine: {
            show: true,
            lineStyle: {
              color: "#d7d7d7",
            },
          },
          axisLabel: {
            color: "#74798B",
            fontSize: this.fontSize(14),
          },
          axisTick: {
            show: false,
          },
        },
        series: series,
      };
      this.chart.setOption(option, true);
    },
  },
};
</script>

<style scope>
a {
  background-color: #1b59f1;
  background-color: #ee0100;
  background-color: #d7d7d7;
}
</style>